
function requiredFieldValidator(value) {
	if (value == null || value == undefined || !value.length) {
		return {valid:false, msg:"This is a required field"};
	} else {
		return {valid:true, msg:null};
	}
}

function selectMultipleRows() {
    if (!Slick.GlobalEditorLock.commitCurrentEdit()) { 
    	return; 
    }
    
    grid.setSelectedRows([0, 1, 2]);
}

function CurrencySelectCellEditor(args) {
	var $select;
    var defaultValue;
    var scope = this;

    this.init = function() {
        $select = $("<SELECT tabIndex='0' id='currency_'></SELECT>");
        
        $select.empty();
        // alert(currency_json);
        $.each(currency_json, function(k, v) {
        	// alert(k + " = " + v);
        	var item = new Option(v, k);
        	$select[0].options.add(item);
        });
        
        $select.appendTo(args.container);
        
        $select.change(function() {
    		var s = $(this).val();
    		if (s != null && s != '-')　{
    			var url = 'finance.php?do=Finance.ajax_exchange&currency=' + s;
    			$.get(url, function(data) {
    				exchange_item = data; 
    			});
    		 }		
    	});
        
        $select.focus();
    };

    this.destroy = function() {
        $select.remove();
    };

    this.focus = function() {
        $select.focus();
    };

    this.loadValue = function(item) {
    	$select.val(item[args.column.field]);
    	$select.select();
    };

    this.serializeValue = function() {
        return $select.val();
    };

    this.applyValue = function(item,state) {    	
        item[args.column.field] = state;
    };
   
    this.isValueChanged = function() {
        return ($select.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}

function CurrencySelectCellEditor2(args) {
	var $select;
    var defaultValue;
    var scope = this;

    this.init = function() {
        $select = $("<SELECT tabIndex='0' id='currency_'></SELECT>");
        
        $select.empty();
        // alert(currency_json);
        $.each(currency_json, function(k, v) {
        	// alert(k + " = " + v);
        	var item = new Option(v, k);
        	$select[0].options.add(item);
        });
        
        $select.appendTo(args.container);        
        $select.focus();
    };

    this.destroy = function() {
        $select.remove();
    };

    this.focus = function() {
        $select.focus();
    };

    this.loadValue = function(item) {
    	$select.val(item[args.column.field]);
    	$select.select();
    };

    this.serializeValue = function() {
        return $select.val();
    };

    this.applyValue = function(item,state) {    	
        item[args.column.field] = state;
    };
   
    this.isValueChanged = function() {
        return ($select.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}

function MoldCellEditor(args) {
    var $input;
    var defaultValue;
    var scope = this;
    var calendarOpen = false;

    this.init = function() {
        $input = $("<INPUT type=text class='editor-text' />");
        $input.appendTo(args.container);
        $input.focus().select();
        
        $input.autocomplete(Test.mold);
        $input.width($input.width() - 18);
    };

    this.destroy = function() {
        $.datepicker.dpDiv.stop(true,true);
        $input.datepicker("hide");
        $input.datepicker("destroy");
        $input.remove();
    };

    this.show = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).show();
        }
    };

    this.hide = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).hide();
        }
    };

    this.position = function(position) {
        if (!calendarOpen) return;
        $.datepicker.dpDiv
            .css("top", position.top + 30)
            .css("left", position.left);
    };

    this.focus = function() {
        $input.focus();
    };

    this.loadValue = function(item) {
        defaultValue = item[args.column.field];
        $input.val(defaultValue);
        $input[0].defaultValue = defaultValue;
        $input.select();
    };

    this.serializeValue = function() {
        return $input.val();
    };

    this.applyValue = function(item,state) {
        item[args.column.field] = state;
    };

    this.isValueChanged = function() {
        return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}

function EmpCellEditor(args) {
    var $input;
    var defaultValue;
    var scope = this;
    var calendarOpen = false;

    this.init = function() {
        $input = $("<INPUT type=text class='editor-text' />");
        $input.appendTo(args.container);
        $input.focus().select();
        
        $input.autocomplete(Test.emp);
        $input.width($input.width() - 18);
    };

    this.destroy = function() {
        $.datepicker.dpDiv.stop(true,true);
        $input.datepicker("hide");
        $input.datepicker("destroy");
        $input.remove();
    };

    this.show = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).show();
        }
    };

    this.hide = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).hide();
        }
    };

    this.position = function(position) {
        if (!calendarOpen) return;
        $.datepicker.dpDiv
            .css("top", position.top + 30)
            .css("left", position.left);
    };

    this.focus = function() {
        $input.focus();
    };

    this.loadValue = function(item) {
        defaultValue = item[args.column.field];
        $input.val(defaultValue);
        $input[0].defaultValue = defaultValue;
        $input.select();
    };

    this.serializeValue = function() {
        return $input.val();
    };

    this.applyValue = function(item,state) {
        item[args.column.field] = state;
    };

    this.isValueChanged = function() {
        return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}

function SupplierCellEditor(args) {
    var $input;
    var defaultValue;
    var scope = this;
    var calendarOpen = false;

    this.init = function() {
        $input = $("<INPUT type=text class='editor-text' />");
        $input.appendTo(args.container);
        $input.focus().select();
        
        $input.autocomplete(Test.supplier);
        $input.width($input.width() - 18);
    };

    this.destroy = function() {
        $.datepicker.dpDiv.stop(true,true);
        $input.datepicker("hide");
        $input.datepicker("destroy");
        $input.remove();
    };

    this.show = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).show();
        }
    };

    this.hide = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).hide();
        }
    };

    this.position = function(position) {
        if (!calendarOpen) return;
        $.datepicker.dpDiv
            .css("top", position.top + 30)
            .css("left", position.left);
    };

    this.focus = function() {
        $input.focus();
    };

    this.loadValue = function(item) {
        defaultValue = item[args.column.field];
        $input.val(defaultValue);
        $input[0].defaultValue = defaultValue;
        $input.select();
    };

    this.serializeValue = function() {
        return $input.val();
    };

    this.applyValue = function(item,state) {
        item[args.column.field] = state;
    };

    this.isValueChanged = function() {
        return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}

function MachineCellEditor(args) {
    var $input;
    var defaultValue;
    var scope = this;
    var calendarOpen = false;

    this.init = function() {
        $input = $("<INPUT type=text class='editor-text' />");
        $input.appendTo(args.container);
        $input.focus().select();
        
        $input.autocomplete(Test.machine);
        $input.width($input.width() - 18);
    };

    this.destroy = function() {
        $.datepicker.dpDiv.stop(true,true);
        $input.datepicker("hide");
        $input.datepicker("destroy");
        $input.remove();
    };

    this.show = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).show();
        }
    };

    this.hide = function() {
        if (calendarOpen) {
            $.datepicker.dpDiv.stop(true,true).hide();
        }
    };

    this.position = function(position) {
        if (!calendarOpen) return;
        $.datepicker.dpDiv
            .css("top", position.top + 30)
            .css("left", position.left);
    };

    this.focus = function() {
        $input.focus();
    };

    this.loadValue = function(item) {
        defaultValue = item[args.column.field];
        $input.val(defaultValue);
        $input[0].defaultValue = defaultValue;
        $input.select();
    };

    this.serializeValue = function() {
        return $input.val();
    };

    this.applyValue = function(item,state) {
        item[args.column.field] = state;
    };

    this.isValueChanged = function() {
        return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}
